package com.hzqy.web.ac.itf;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.hzqy.commons.utils.AESUtil;
import com.hzqy.commons.utils.MD5Util;
import com.hzqy.service.statistics.VisitServiceBean;
import com.hzqy.web.vo.StatDataAdvertisingStatisticsVo;
import org.apache.http.HttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hzqy.commons.utils.HttpUtil;
import com.hzqy.commons.utils.LvYunUtil;
import com.hzqy.service.ift.HotelItfServiceBean;
import com.hzqy.service.wailian.WaiLianServiceBean;
import com.hzqy.web.vo.WlContentVo;

/**
 * 接口API，hotel平台业务使用
 *
 */
@Controller
@RequestMapping("/itf/hotelItf")
public class HotelItfAction {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	
	@Resource
	private HotelItfServiceBean hotelItfServiceBean;
	
	@Resource
	private WaiLianServiceBean waiLianServiceBean;

	@Resource
	private VisitServiceBean visitServiceBean;
	
	/**
	 * 绿云接口
	 *
	 */
	@RequestMapping("/getWelcome")
	@ResponseBody
	public String getWelcome(HttpSession session,String id,String welcomeText, String hotel){
		LvYunUtil ly = new LvYunUtil();
		String str = ly.getWelcome(id,welcomeText,hotel);
		return str;
	}
	
	/**
	 * 根据serverid获取产品包的价格(已废弃 转用前台的)
	 *详细说明请查看dbchange->word->酒店完美联盟接口开发文档
	 */
	@RequestMapping("/getProduct")
	@ResponseBody
	public Map<String,Object> getProduct(HttpSession session,String sign,String serverid,String timestamp,String secret,String source){
		Map<String,Object> map =new HashMap<String,Object>();
		long t1 = System.currentTimeMillis();
		LOG.debug("开始获取产品包价格接口 serverid=" + serverid);
		map = hotelItfServiceBean.getProduct(sign,serverid,timestamp,secret,source);
		LOG.debug("结束获取产品包价格接口 serverid=" + map + " 处理耗时: " + (System.currentTimeMillis() - t1) + "ms");
		return map;
	}
	
	/**
	 * 绿云实时接口  导入各房间入住信息
	 * @throws IOException 
	 */
	@RequestMapping("/lds_hotel_message")
	@ResponseBody
	public Map<String,Object> updateLDSHotetMessage(HttpServletRequest request){
		Map<String,Object> map =new HashMap<String,Object>();
		int f_wlc_id = 847;//绿云雷迪森维嘉酒店接口外链id
		String sysConfigKey = "control_link_ab";
		WlContentVo wlContentVo = new WlContentVo();
		wlContentVo.setF_wlc_id(f_wlc_id);
		wlContentVo.setSysConfigKey(sysConfigKey);
		WlContentVo wlc = waiLianServiceBean.selectWLContentAvaliable(wlContentVo);
		if(wlc==null){//不能访问
			LOG.debug("禁止访问绿云实时接口");
			map.put("rescode", "101");
			map.put("resDesc", "禁止访问绿云实时接口");
		}else{
			BufferedReader reader;
			String message = "" ;
			try {
				reader = request.getReader();
				char[] buf = new char[1024];
				int len = 0;
				StringBuffer contentBuffer = new StringBuffer();
				while ((len = reader.read(buf)) != -1) {
					contentBuffer.append(buf, 0, len);
				}
				message = contentBuffer.toString();
			} catch (IOException e) {
				LOG.error("绿云实时接口失败："+e);
			}
			if(message == null){
				message = "";
			}
			
			long t1 = System.currentTimeMillis();
			LOG.debug("开始绿云实时接口message:"+message);
			map = hotelItfServiceBean.updateLDSHotetMessage(message);
			LOG.debug("结束绿云实时接口" + map + " 处理耗时: " + (System.currentTimeMillis() - t1) + "ms");
		}
		
		return map;
	}
	
	/**
	 * 西软实时接口  导入各房间入住信息
	 * @throws IOException 
	 */
	@RequestMapping("/xr_hotel_message")
	@ResponseBody
	public Map<String,Object> updateXRHotetMessage(HttpServletRequest request){
		Map<String,Object> map =new HashMap<String,Object>();
		int f_wlc_id = 853;//西软实时接口
		String sysConfigKey = "control_link_ab";
		WlContentVo wlContentVo = new WlContentVo();
		wlContentVo.setF_wlc_id(f_wlc_id);
		wlContentVo.setSysConfigKey(sysConfigKey);
		WlContentVo wlc = waiLianServiceBean.selectWLContentAvaliable(wlContentVo);
		if(wlc==null){//不能访问
			LOG.debug("禁止访问西软实时接口");
			map.put("rescode", "101");
			map.put("resDesc", "禁止访问西软实时接口");
		}else{
			BufferedReader reader;
			String message = "" ;
			try {
				reader = request.getReader();
				char[] buf = new char[1024];
				int len = 0;
				StringBuffer contentBuffer = new StringBuffer();
				while ((len = reader.read(buf)) != -1) {
					contentBuffer.append(buf, 0, len);
				}
				message = contentBuffer.toString();
			} catch (IOException e) {
				LOG.error("西软实时接口失败："+e);
			}
			if(message == null){
				message = "";
			}
			
			long t1 = System.currentTimeMillis();
			LOG.debug("开始西软实时接口message:"+message);
			map = hotelItfServiceBean.updateXRHotetMessage(message);
			LOG.debug("结束西软实时接口" + map + " 处理耗时: " + (System.currentTimeMillis() - t1) + "ms");
		}
		
		return map;
	}
	
	/**
	 * 点播分成用户订购成功的回调接口
	 */
	@PostMapping("/demandUserOrderRecord")
	@ResponseBody
	public Map<String,Object> demandUserOrderRecord(HttpServletRequest request){
		LOG.debug("开始接收订购记录");
		String message = "";
		try {
			message = HttpUtil.inputStream2String(request.getInputStream());
			LOG.debug("message:"+message);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JSONObject object = JSONArray.parseObject(message);
		Map<String,Object> resultMap = hotelItfServiceBean.demandUserRecord(object);
		LOG.debug("订购记录结果:"+resultMap);
		return resultMap;
	}
	
//	/**
//	 * 广告日统计数据导入接口
//	 */
//	@PostMapping("/advertisingDayStatistics")
//	@ResponseBody
//	public Map<String,Object> advertisingDayStatistics(HttpServletRequest request){
//		LOG.debug("开始接收广告日统计数据");
//		String message = "";
//		try {
//			message = HttpUtil.inputStream2String(request.getInputStream());
//			LOG.debug("message:"+message);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		JSONObject object = JSONArray.parseObject(message);
//		Map<String,Object> resultMap = hotelItfServiceBean.advertisingDayStatistics(object);
//		LOG.debug("接收广告日统计数据结果:"+resultMap);
//		return resultMap;
//	}
//
//	/**
//	 * 各平台广告日统计数据导出接口
//	 */
//	@RequestMapping("/export_advertisingDayData")
//	@ResponseBody
//	public List<StatDataAdvertisingStatisticsVo> exportAdvertisingDayData(StatDataAdvertisingStatisticsVo statDataAdvertisingStatisticsVo, HttpServletRequest request,String sign,String timestamp){
//		LOG.debug("各平台广告日统计数据导出接口传入参数："+request.getQueryString());
//		List<StatDataAdvertisingStatisticsVo> list= hotelItfServiceBean.exportAdvertisingDayData(statDataAdvertisingStatisticsVo,sign,timestamp);
//		return list;
//	}
}
